package it.getta.vicinometro.popup;

import it.getta.vicinometro.ActivityCallBack;
import it.getta.vicinometro.R;
import it.getta.vicinometro.adapter.PlacesPopupAdapter;
import it.getta.vicinometro.adapter.PlacesPopupAdapter.OnInfoPressedListener;
import it.getta.vicinometro.adapter.PlacesPopupAdapter.OnSetAlarmPressedListener;
import it.getta.vicinometro.model.Place;
import it.getta.vicinometro.model.PlaceList;
import it.getta.vicinometro.places.NetworkListener;
import it.getta.vicinometro.places.NetworkManager;
import it.getta.vicinometro.utils.ExpandAnimation;
import it.getta.vicinometro.utils.FileCache;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;

public class ChooseFromListPopup extends VicinometroPopup {
	
	
	private PlacesPopupAdapter mAdapter;
	private PlaceList mInterestsList;
	private GeoPoint mPoint;
	private PlaceList mCachedPlaces;
	private FileCache mFileCache;
	
	public ChooseFromListPopup(Context context, ActivityCallBack activityCallBack, GeoPoint location, FileCache fileCache) {
		super(context, activityCallBack, R.layout.choose_from_list_popup);
		mPoint = location;
		mFileCache = fileCache;
	}
	
	public ChooseFromListPopup(Context context, ActivityCallBack activityCallBack, GeoPoint location, PlaceList cachedPlaces, FileCache fileCache) {
		super(context, activityCallBack, R.layout.choose_from_list_popup);
		mPoint = location;
		mCachedPlaces = cachedPlaces;
		mFileCache = fileCache;
	}

	@Override
	public void showPopup() {
		super.showPopup();
		mInterestsList = new PlaceList();
		
		final ListView list = (ListView)mPopupRootView.findViewById(R.id.choose_from_list_list);
		LinearLayout progress = (LinearLayout)mPopupRootView.findViewById(R.id.choose_from_list_progress_container);
		list.setEmptyView(progress);
		mAdapter =  new PlacesPopupAdapter(mContext, mInterestsList, mFileCache);
		mAdapter.setOnInfoPressedListener(new OnInfoPressedListener() {
			@Override
			public void infoPressed(Place place) {
				dismiss();
				mActivityCallBack.showInfoPopup(place.getReference());
			}
		});
		mAdapter.setOnSetAlarmPressedListener(new OnSetAlarmPressedListener() {
			
			@Override
			public void setAlarmPressed(Place place) {
				mActivityCallBack.showSetAlarmPopup(mPoint);
				dismiss();
			}
		});
		list.setAdapter(mAdapter); 
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, final int pos,
					long lpos) {
				View toolbar = view.findViewById(R.id.place_list_item_toolbar);
                ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500);
                expandAni.setAnimationListener(new AnimationListener() {
					
					@Override
					public void onAnimationStart(Animation animation) {
						
					}
					
					@Override
					public void onAnimationRepeat(Animation animation) {
						
					}
					
					@Override
					public void onAnimationEnd(Animation animation) {
//						Utils.hardLong("onAnimationEnd");
//						if(pos == mAdapter.getCount() - 1) {
//							Utils.hardLong("postInvalidate");
//							list.postInvalidate();
//							
//		                }
					}
				});
                toolbar.startAnimation(expandAni);
                
                
			}
		});
		TextView latitude = (TextView)mPopupRootView.findViewById(R.id.choose_from_list_latitude);
		TextView longitude = (TextView)mPopupRootView.findViewById(R.id.choose_from_list_longitude);
		latitude.setText("" + mPoint.getLatitudeE6()/1e6);
		longitude.setText("" + mPoint.getLongitudeE6()/1e6);
		if (mCachedPlaces == null) {
			fillPlaces();
		} else {
			refreshPlaces(mCachedPlaces);
			mActivityCallBack.hideAsyncLoading();
		}
		
	}
	
	private void fillPlaces() {
		NetworkManager.getInstance().getPlacesSearchTask(mPoint, new NetworkListener() {
			@Override
			public void getPlacesSearchPerformed(final PlaceList places) {
				mActivityCallBack.fillCachedPlaces(places);
				refreshPlaces(places);
				mActivityCallBack.hideAsyncLoading();
			}
			@Override
			public void getPlacesSearchFailed(Exception e) {
				e.printStackTrace();
				mActivityCallBack.hideAsyncLoading();
				showNetworkMessage();
			}
			@Override
			public void getPlacesDetailFailed(int statusCode) {
				mActivityCallBack.hideAsyncLoading();
				showNetworkMessage();
			}
			
			private void showNetworkMessage() {
				mHandler.post(new Runnable() {
					@Override
					public void run() {
						mActivityCallBack.showNetworkAlert();
					}
				});
				
			}
			
		});
	}
	
	private void refreshPlaces(PlaceList places) {
		List<Place> placeList = new ArrayList<Place>();
		for(Place p: places.getResults()) {
//			System.out.println("Place = " + p.toString());
//			System.out.println("Place name = " + p.getName());
//			System.out.println("Place vicinity = " + p.getVicinity());
//			System.out.println("Place types = " + p.getTypes().toString());
//			System.out.println("Place location : lat = " + p.getGeometry().getLocation().getLat() + ", lng = " + p.getGeometry().getLocation().getLng());
			placeList.add(p); 
		}
		mInterestsList.setResults(placeList);
		mHandler.post(new Runnable() {
			
			@Override
			public void run() {
				
				mAdapter.notifyDataSetChanged();
				show(mActivityCallBack.getPopupAnchor());
				
			}
		});
	}

}
